#include "queueexp.h"
#include"filemanage.h"

QueueExp::QueueExp()
{
    bool state=FM->LoadFromFile(FileManage::GetQueueFile(),&filePaths);
    foreach(QString filePath,filePaths)
    {
        if(QFile::exists(filePath)==false)
        {
            RemovePath(filePath);//
            continue;
        }
        Experiment exp;
        state=FM->LoadFromFile(filePath,&exp);
        exps.insert(filePath,exp);
    }
}

QHash<QString,Experiment> QueueExp::GetQueueExps()
{
   return exps;
}
QStringList QueueExp::GetFilePaths()
{
   return filePaths;
}

bool QueueExp::RemovePath(QString filePath)
{
    if(filePaths.contains(filePath)==false) return false;
    filePaths.removeOne(filePath);
    SaveQueue();
}
bool QueueExp::AddPath(QString filePath)
{
   if(filePaths.contains(filePath)) return false;
   filePaths.append(filePath);
   SaveQueue();
}

bool QueueExp::RemoveExp(QString filePath)
{
    if(filePaths.contains(filePath)==false) return false;

    filePaths.removeOne(filePath);
    SaveQueue();

    SaveExp(filePath,exps.take(filePath));

    return true;
}
bool QueueExp::AddExp(QString filePath,Experiment exp)
{
    if(filePaths.contains(filePath)==false)
    {
        filePaths.append(filePath);
    }
    exps.insert(filePath,exp);

    SaveQueue();
    SaveExp(filePath,exp);

    return true;
}
void QueueExp::init()
{

}

bool QueueExp::SaveQueue()
{
    bool state=FM->SaveToFile(FileManage::GetQueueFile(),&filePaths);
}
bool QueueExp::SaveExp(QString filePath,Experiment exp)
{
    bool state=FM->SaveToFile(filePath,&exp);
}

Experiment QueueExp::GetExp(QString filePath)
{
    return exps.value(filePath);
}
Experiment* QueueExp::GetExpPtr(QString filePath)
{
    return &exps[filePath];
}
QDataStream & operator<< ( QDataStream & stream, const QueueExp & queueExp )
{
    stream<<queueExp.filePaths;
}
QDataStream &	operator>> ( QDataStream & stream, QueueExp & queueExp )
{
    stream<<queueExp.filePaths;
}
